/**
 *  This file is part of TaoBaoItemImporter.
 *
 *   TaoBaoItemImporter is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   TaoBaoItemImporter is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with TaoBaoItemImporter.  If not, see <http://www.gnu.org/licenses/>. 
 */
package com.googlecode.ulysses.taobao.importer;

import java.util.ArrayList;

import org.apache.commons.lang3.StringUtils;

import com.googlecode.ulysses.taobao.Constants;
import com.googlecode.ulysses.taobao.Constants.ItemType;
import com.googlecode.ulysses.taobao.data.Item;
import com.googlecode.ulysses.taobao.data.PropertyNameParser;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;

/**
 * @author Ulysses <robman1070@gmail.com>
 *
 */
public class BagItemImporter extends ItemImporter {
	private ItemType type = ItemType.ITEM_TYPE_BAG;
	
	public DBCollection addItemPropertyToCollection(DBCollection coll, Item item) {
		BasicDBObject itemProperties = new BasicDBObject();
		doc.put("item_properties", itemProperties);
		for(String key: Constants.PROPERTY_MAP.getBagMapper().keySet() ) {
			String property = Constants.PROPERTY_MAP.getBagMapper().get(key);
			String dummy = key;
			PropertyNameParser propNameParser = new PropertyNameParser(item.getItem_get_response().getItem().getProps_name());
			propNameParser.CreatePidVidValue(";", ":");
			itemProperties.put(property, StringUtils.join(propNameParser.getPropertyValue(dummy), Constants.PROPERTY_SEPARATOR));
		}
		return coll;
	}
	
	public DBCollection addItemBuyLinkToCollection(DBCollection coll, Item item) {
		ArrayList<BasicDBObject> buyLinks = new ArrayList<BasicDBObject>();
		BasicDBObject buyLink = new BasicDBObject();
		buyLink.put("url", item.getItem_get_response().getItem().getDetail_url()); // FIXME figure out a way to generate CPS url 
		buyLink.put("website", Constants.DEFAULT_WEBSITE);
		buyLink.put("merchant", item.getItem_get_response().getItem().getNick());
		buyLink.put("price", item.getItem_get_response().getItem().getPrice());
		buyLink.put("cash_back", Constants.DEFAULT_CASH_BACK);
		buyLink.put("is_sold_out", isSoldOut(item));
		buyLinks.add(buyLink); 
		doc.put("buy_link", buyLinks); 
		//System.out.println(cpsurl);
		
		return coll;
	}

	/* (non-Javadoc)
	 * @see com.googlecode.ulysses.taobao.importer.ItemImporter#addAndUpdateItemToCollection(com.mongodb.DBCollection, com.googlecode.ulysses.taobao.data.Item)
	 */
	@Override
	public DBCollection addAndUpdateItemFieldToCollection(DBCollection coll,
			Item item) {
		doc.put("item_main_cats", type.getItemTypeNumber());
		return coll;
	}

}
